package QuizUI;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import message.*;
import social.SocialDBC;

/**
 * Servlet implementation class ChallengeServlet
 */
@WebServlet("/ChallengeServlet")
public class ChallengeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChallengeServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SocialDBC db = (SocialDBC) getServletContext().getAttribute("database");

		String action = request.getParameter("action");
		String toUser = request.getParameter("toUser");
		String fromUser = request.getParameter("fromUser");
		int id = Integer.parseInt(request.getParameter("id"));
		HttpSession session = request.getSession();

		if (action.equals("accept")) {
			//redirect to quiz page
			//remove quiz challenge
			AbstractMessage challenge = db.getMailById(id);
			//System.out.println("challenge:" + challenge.getMessage());
			int quizID = Integer.parseInt(challenge.getMessage());

			request.setAttribute("profileName", session.getAttribute("username"));
			request.setAttribute("quizid", challenge.getMessage());
			db.removeMail(id);
			request.getRequestDispatcher("StartQuiz.jsp").forward(request, response);
		} else if (action.equals("reject")) {
			//delete quiz challenge redirect to mailbox
			db.removeMail(id);

			request.setAttribute("profileName", session.getAttribute("username"));
			request.getRequestDispatcher("mailbox.jsp").forward(request, response);
		}		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();

		SocialDBC db = (SocialDBC) getServletContext().getAttribute("database");
		String fromUser = (String) session.getAttribute("username");
		String toUser = request.getParameter("friendName");
		String quizID = request.getParameter("quizid");
		//System.out.println("quizID:" + quizID);
		if (toUser==null) {
			//System.out.println("user null");
			request.setAttribute("profileName", fromUser);
			request.setAttribute("quizid", quizID);
			request.getRequestDispatcher("StartQuiz.jsp").forward(request, response);
		} else {
			db.addMail(toUser, fromUser, quizID, "challenge");

			request.setAttribute("profileName", fromUser);
			request.setAttribute("quizid", quizID);
			request.getRequestDispatcher("StartQuiz.jsp").forward(request, response);
		}
	}

}
